// pages/group/add.js
import {upfile,postgroup} from '../../utils/api';
Page({
  /**
   * 页面的初始数据
   */ 
  data: {
    show: false,
    stashow:false,
    endshow:false,
    list:[],
    filelist:[],
    statime: new Date().getTime(),
    minDate: new Date().getTime(),
    statime1: new Date().getTime(),
    minDate1: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  onInput(event) {
    const date= new Date(event.detail);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    this.setData({
      currentDate: event.detail,
      statimes:year+'/'+month+'/'+day,
      statime:event.detail/1000
    });
  },
  onInput1(event) {
    const date= new Date(event.detail);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    this.setData({
      currentDate1: event.detail,
      endtimes:year+'/'+month+'/'+day,
      endtime:event.detail/1000
    });
  },
  dele(){
    var that=this;
    that.setData({
      filelist:[]
    })
  },
  txtchange(e){
     
     var that=this
     if(e.type=='change'){
      var value=e.detail
     }else if(e.type=='click'){
      var value= e.target.dataset.name
     }else{
       var value=e.detail.value
     }
     var tlin=e.target.dataset.id
     that.setData({
       [tlin]:value
     })
    
  },
  viewimg(e){
    console.log(e)
    var that=this;
    var filelist=[];
    const { file } = e.detail;
    upfile(file).then(data=>{
        var d=JSON.parse(data);
        var newfile={
          url:d.data.fullurl,
        };
        filelist.push(newfile);
        that.setData({
          filelist:filelist
        })
    })
   },
  addc(e){
    var that=this
    var list=that.data.list
    var newc={
      tit:'',
      tpla:"请输入名称",
      npla:"请输入目标数",
      num:100
    }
    list.push(newc)
    that.setData({
    list:list
  })
  },
  pgroup(){
    console.log(this.data)
    var that=this
    var url=that.data.filelist.length==0?'':that.data.filelist[0].url
    var pdata={
      name:that.data.name==undefined?'':that.data.name,
      desc:that.data.desc==undefined?'':that.data.desc,
      type:that.data.type==undefined?'':that.data.type,
      image:url,
      statime:that.data.statime==undefined?'':that.data.statime,
      endtime:that.data.endtime==undefined?'':that.data.endtime,
      numjson: JSON.stringify(that.data.list),
      huixiang:that.data.huixiang==undefined?'':that.data.huixiang,
    }
    postgroup(pdata).then(res=>{
      wx.showToast({
        title: res.msg,
        icon:"none"
      })
      if(res.code==1){
       wx.switchTab({
          url:"/pages/index/index",
        })
      }
    })
  },
  onClickHide(){
    this.setData({stashow:false})
  },
  stashows(){
    this.setData({stashow:true})
  },
  endshows(){
    this.setData({endshow:true})
  },
  onClickHide1(){
    this.setData({endshow:false})
  },
  caschange(e){
    var that=this
    var index=e.target.dataset.id
    var value=e.detail
    var list=that.data.list
    list[index].tit=value
    that.setData({
      list:list
    })
  },
  casnchange(e){
    var that=this
    var index=e.target.dataset.id
    var value=e.detail
    var list=that.data.list
    list[index].num=value
    that.setData({
      list:list
    })
  },
  // onDisplay() {
  //   this.setData({show:true});
  // },
  // onClose() {
  //   this.setData({ show: false });
  // },
  // formatDate(date) {
  //   date = new Date(date);
  //   return `${date.getMonth() + 1}/${date.getDate()}`;
  // },
  // onConfirm(event) {
  //   console.log(event)
  //   const [start, end] = event.detail;
 
  //   this.setData({
  //     show: false,
  //     date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
  //     statime:start.getTime()/1000,
  //     endtime:end.getTime()/1000
  //   });
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})